package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.yearMaster.UserYearMasterData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserYearMasterDaoImpl extends TempDataObjectDao<UserYearMasterData> {


    public Map<Integer, UserYearMasterData> getUserYearMasterInfo(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserYearMasterData> userYearMasterMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_activity_year_master where userId =?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserYearMasterData result = parse(rs);
                userYearMasterMap.put(result.getActivityId(), result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userYearMasterMap;
    }


    public static String updateUserYearMasterDataSql = "update t_u_activity_year_master set physicalStrength=?,score=?,paint=?,highPaint=?,fastPaint=?,strengthTime=?,lastUpdateTime=? where userId=? and activityId=?";

    @Override
    protected boolean updateData(UserYearMasterData userYearMasterData) {
        userYearMasterData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userYearMasterData.getPhysicalStrength()));
        para.put(i++, new DbParameter(Types.BIGINT, userYearMasterData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYearMasterData.getPaint()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYearMasterData.getHighPaint()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYearMasterData.getFastPaint()));
        para.put(i++, new DbParameter(Types.BIGINT, userYearMasterData.getStrengthTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userYearMasterData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userYearMasterData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userYearMasterData.getActivityId()));
        if (getDb().executeNoneQuery(updateUserYearMasterDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserYearMasterDataSql = "insert into t_u_activity_year_master(userId,activityId,physicalStrength,score,paint,highPaint,fastPaint,strengthTime,lastUpdateTime) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserYearMasterData userYearMasterData) {
        userYearMasterData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userYearMasterData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userYearMasterData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, userYearMasterData.getPhysicalStrength()));
        para.put(i++, new DbParameter(Types.BIGINT, userYearMasterData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYearMasterData.getPaint()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYearMasterData.getHighPaint()));
        para.put(i++, new DbParameter(Types.VARCHAR, userYearMasterData.getFastPaint()));
        para.put(i++, new DbParameter(Types.BIGINT, userYearMasterData.getStrengthTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userYearMasterData.getLastUpdateTime()));
        if (getDb().executeNoneQuery(insertUserYearMasterDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserYearMasterData parseData(ResultSet rs) throws SQLException {
        UserYearMasterData userYearMasterData = new UserYearMasterData();
        userYearMasterData.setUserId(rs.getLong("userId"));
        userYearMasterData.setActivityId(rs.getInt("activityId"));
        userYearMasterData.setPhysicalStrength(rs.getInt("physicalStrength"));
        userYearMasterData.setScore(rs.getLong("score"));
        userYearMasterData.setPaint(rs.getString("paint"));
        userYearMasterData.setHighPaint(rs.getString("highPaint"));
        userYearMasterData.setFastPaint(rs.getString("fastPaint"));
        userYearMasterData.setStrengthTime(rs.getLong("strengthTime"));
        userYearMasterData.setLastUpdateTime(rs.getLong("lastUpdateTime"));

        return userYearMasterData;
    }
}
